using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace TileGert
{
  /// <summary>
  /// GertLogger is a simple generic logging engine. It supports logging levels to be able to control what information is logged.
  /// All Information that is automatically logged to a text file, an internal log and the debug output, that can be read
  /// in the Visual Studio IDE when running the application.  /// 
  /// </summary>
  public class GertLogger
  {
    private StreamWriter _writer = null;
    private StringBuilder _builder = new StringBuilder();
    private int _level = 0;
    private string _format = "{0} ({1}): {2}";

    /// <summary>
    /// Creates a new logger that writes to the given file. If the file does not exist, it is created, else the file will be appended.
    /// </summary>
    /// <param name="filename">The name and path of the file to write the log to.</param>
    public GertLogger(string filename)
    {
      _writer = File.AppendText(filename);
    }

    /// <summary>
    /// Gets the current timestamp as a string.
    /// </summary>
    private string CurrentTimestamp
    {
      get { return DateTime.Now.ToLongTimeString(); }
    }

    /// <summary>
    /// Gets the log text. Only the text that was logged since the logger was created is returned.
    /// </summary>
    public string Text
    {
      get { return _builder.ToString(); }
    }

    /// <summary>
    /// Sets the current log level. The default is 0.
    /// Only messages with a log level >= this number are logged.
    /// </summary>
    public int LogLevel
    {
      get { return _level; }
      set { _level = value; }
    }

    /// <summary>
    /// Appends the given text to the log file, internal log and the debug output.
    /// The text is prefixed with the current timestamp and the level of the given level.
    /// </summary>
    /// <param name="text">The text to be logged.</param>
    /// <param name="level">The log level of the log text.</param>
    public void Log(string text, int level)
    {
      if (level >= _level)
      {
        string logText = string.Format(_format, CurrentTimestamp, level, text);
        _writer.Write(logText);
        _builder.Append(logText);
        System.Diagnostics.Debug.Write(logText);
      }
    }

    /// <summary>
    /// Appends the given text in a new line to the log file, internal log and the debug output.
    /// The text is prefixed with the current timestamp and the level of the given level.
    /// </summary>
    /// <param name="text">The text to be logged.</param>
    /// <param name="level">The log level of the log text.</param>
    public void LogLine(string text, int level)
    {
      if (level >= _level)
      {
        string logText = string.Format(_format, CurrentTimestamp, level, text);
        _writer.WriteLine(logText);
        _builder.AppendLine(logText);
        System.Diagnostics.Debug.WriteLine(logText);
      }
    }

  }
}
